import api from "@/api/api";
import { wxUpImg, wxChooseImage } from '@/util/uniapi'
export default {
    data() {
        return {
            title: "hello",
            upimg: {
                pnum: '',
                sign: '',
                uid: '',
            },//上传小票配置
            upurl: 'https://file02.esurl.cn/nestle/2020/sm1833/images/btn_upimg.png',
        };
    },


    async onReady() {



    },
    async onLoad() {
        this.getOssKey()
    },
    // async created() {
    //     this.getOssKey()
    // },
    methods: {
        async chooseImg() {
            let that=this;
            // #ifdef  MP-WEIXIN
            let tempFilePath = await wxChooseImage();
            let fileParam = await wxUpImg(that.ossUrl, tempFilePath.tempFilePaths[0]);
            this.upurl = fileParam.ResultData.list[0].OriginImgUrl;
            let imgparam = {
                FileName: fileParam.ResultData.list[0].ImgUrl,
                FileHash: fileParam.ResultData.list[0].ImgSign,
                OriginFileName: fileParam.ResultData.list[0].OriginImgUrl,
              }
              
            // #endif


            // #ifdef H5
              let localIds = await this.$wechat.chooseImage()
              let serverId = await this.$wechat.wxUploadImage(localIds)
              this.wxPostOss(serverId)
            // #endif
        },
        // 上传图片到oss系统
        async wxPostOss(serverId) {
        console.log(this.upimg,'this.upimg')
          let params = {
            mediaIds: [serverId],
            Pnum: this.upimg.Pnum,
            Sign: this.upimg.Sign,
            Uid: this.upimg.Uid,
          }
          let res = await this.$post(api.ossUrlWxh5,params)
        //   console.log(res,res是什么呢)
          if(res.IsSuccess) {
            let imgparam = {
                FileName: fileParam.ResultData.list[0].ImgUrl,
                FileHash: fileParam.ResultData.list[0].ImgSign,
                OriginFileName: fileParam.ResultData.list[0].OriginImgUrl,
            }
            this.upurl = res.ResultData.list[0].OriginImgUrl
          }
        },
        //获取oss配置
        async getOssKey() {
            let that = this;
            console.log('api', api.GetOssKey)
            let res = await this.$post(api.GetOssKey);
            console.log(res,'resmmmmmmmm')
            // let result = JSON.parse(res)
            // console.log(result,'resmmmmmmmm')
            if (res.Success) {
                this.upimg = res.Data;
                this.ossUrl = api.ossUrlSp + '?pnum=' + this.upimg.Pnum + '&sign=' + this.upimg.Sign + '&uid=' + this.upimg.Uid;
                console.log(this.ossUrl,'this.ossUrl')
            } else {
                uni.showToast({
                    title: res.Message,
                    icon: 'none'
                })
            }
        },
    },


};